 
// axios二次封装 
import axios from "axios";
// const serverurl = 'https://shop.kystu.cn/prod-api' 
const serverurl = 'http://localhost:8080' 
export  function request(config) {
  const instance = axios.create({
	changeOrigin: true,// 跨域，这个好象真假都行
	withCredentials: false, // 跨域，这个必须为false
	timeout: 15000, // request timeout
	httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT', 'POST'],
	// headers: { 'Content-Type': 'application/json' },
	 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
  }); 
   axios.defaults.timeout = 10000   
   axios.defaults.withCredentials = true// 跨域资源允许携带
   axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
  instance.interceptors.request.use(config=>{
    config.headers['Authorization'] = 'Bearer ' + sessionStorage.getItem('token')
    return config},error=>{return Promise(()=>{})})
  instance.interceptors.response.use(response => {// 添加响应拦截器
    return response.data; 
  }, error => {// 请求错误时做些事
    let status = error.code;
    if (status) {
      switch (status) {
        case 400:
          error.message = "请求错误(400)";
          break;
        case 401:
          error.message = "未授权，请重新登录(401)";
          // this.router.push("/login");
          break;
        case 403:
          error.message = "拒绝访问(403)";
          break;
        case 404:
          error.message = "请求出错(404)";
          // this.router.push("/404");
          break;
        case 408:
          error.message = "请求超时(408)";
          break;
        case 500:
          error.message = "服务器错误(500)";
          // this.router.push("/a500");
          break;
        case 501:
          error.message = "服务未实现(501)";
          break;
        case 502:
          error.message = "网络错误(502)";
          break;
        case 503:
          error.message = "服务不可用(503)";
          break;
        case 504:
          error.message = "网络超时(504)";
          break;
        case 505:
          error.message = "HTTP版本不受支持(505)";
          break;
        default:
          error.message = `连接出错!`;
      }
	  console.log(error.message)
    } else {
      error.message = "连接服务器失败!,这里";
    }
    return Promise.reject(error);
  });  
  return instance(config)
} 
export function getdata(myurl, mydata, mymethod = 'GET') {
		return request({
			method: mymethod,
			url: serverurl+myurl,
			params: mydata
		})
}
export function execdata(myurl, mydata, mymethod = 'POST') {
		return request({
		  url: serverurl+myurl,
		  method: mymethod,
		  data: mydata,
			headers: { 'Content-Type': 'application/json;charset=UTF-8' },
		})
}
